var app = getApp();
var custId = app.globalData.custID;
var common = require("../../utils/Conmon.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    flag1: false,
    pageIndex: 1,
    pageSize: 10,
    list: [],
    _num: 1,  //1销量 2价格
    flag2: true,
    pageNum: 0,
    keyword: "",
    catalogId: 0,
    shopID:0
  },
  changShowType: function () {
    this.setData({
      flag1: !this.data.flag1
    })
  },
  gosearch: function () {
    wx.navigateTo({
      url: '/pages/search/search',
    })
  },
  changeOrder: function (e) {
    this.setData({
      _num: e.target.dataset.num,
      flag2: !this.data.flag2,
      pageIndex: 1
    })
    loadProduct(this, 0);
  },
  changeKeyword: function (name) {
    this.setData({
      keyword: name
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    common.pageInit(function () { });
    var shopID=0;
    if (options.shopID>0)
    {
      shopID = options.shopID;
    }
    this.setData({
      keyword: options.keyword,
      catalogId: options.CatalogId,
      shopID: shopID
    })
    loadProduct(this, 0);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function (e) {
    var pindex = this.data.pageIndex;
    var pnum = this.data.pageNum;
    if (pindex < pnum) {
      this.setData({
        pageIndex: pindex + 1
      })
      loadProduct(this, 1);
    }
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function (e) {

  }
})

function loadProduct(that, typeid) {  //typeid  0重新加载，1分页加载
  wx.showToast({
    title: '加载中...',
    icon: 'loading'
  })
  wx.request({
    url: app.globalData.apiurl + '/mobile/api/Product/SearchProductInfo',
    data: getData(that),
    method: 'POST',
    header: {
      'X_ManuName': app.globalData.manuName
    },
    success: function (res) {
      wx.hideToast();
      if (res.data.code == 1200) {
        var list = res.data.content.Data;
        if (typeid == 1) {
          that.setData({
            list: that.data.list.concat(list),
            pageNum: res.data.content.PageNum
          });
        } else {
          that.setData({
            list: list,
            pageNum: res.data.content.PageNum
          });
        }
      }
    }
  })
}

function getData(that) {
  var data = { PageIndex: that.data.pageIndex, PageSize: that.data.pageSize, Status: -1, CustID: custId, KeyWord: that.data.keyword, UnionStoreID: that.data.shopID };
  if (that.data.catalogId != 0) {
    data.ProductCatalog = that.data.catalogId;
  }
  if (that.data._num == 1) {//判断排序 销量
    data.OrderBy = 1;
  } else {
    data.OrderBy = 2;
  }
  if (!that.data.flag2) {
    data.OrderByType = 0; //升序
  } else {
    data.OrderByType = 1; //降序
  }
  return data;
}